﻿using Application.Common.Interfaces;
using Application.Features.Users.DTOs;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using Common.Attributes;
using MediatR;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel;

namespace Application.Features.Users.Queries.GetAll;

/// <summary>
/// 查询全部用户
/// </summary>
[Description("全部查询")]
[GroupDescription(("用户管理"))]
public class GetAllUsersQuery : IRequest<IEnumerable<UserOutput>>
{
}

public class GetAllUsersQueryHandler :
    IRequestHandler<GetAllUsersQuery, IEnumerable<UserOutput>>
{
    private readonly IApplicationDbContext _context;
    private readonly IMapper _mapper;

    public GetAllUsersQueryHandler(
        IApplicationDbContext context,
        IMapper mapper
    )
    {
        _context = context;
        _mapper = mapper;
    }

    public async Task<IEnumerable<UserOutput>> Handle(GetAllUsersQuery request, CancellationToken cancellationToken)
    {
        return await _context.Users
            .ProjectTo<UserOutput>(_mapper.ConfigurationProvider)
            .ToListAsync(cancellationToken);
    }
}
